import QtQuick 2.15
import QtQuick.Controls 2.15

Item {
    id: numberInput
    width: inputWidth
    height: inputHeight

    // 基本属性
    property real inputWidth: 200
    property real inputHeight: 50
    property string text: ""
    property string placeholderText: "点击输入"
    property bool isPassword: false
    property var keyboard: null
    property bool isSelected: false

    // 新增：数值限制属性
    property real minValue: -9999  // 下限默认值
    property real maxValue: 9999   // 上限默认值

    // 编辑完成信号，用于传递值
    signal editingFinished(string value)

    // 选中状态变化信号
    signal selectedChanged()

    // 样式属性
    property string normalColor: "#ffffff"
    property string selectedColor: "#FFFACD"
    property string normalBorderColor: "#bdc3c7"
    property string selectedBorderColor: "#FFD700"
    property string hoverBorderColor: "#3498db"
    property real fontPixelSize: 30
    property string fontColor: "#2c3e50"
    property real borderWidth: 3
    property real borderRadius: 5
    property string passwordMask: "*"

    // 悬停状态
    property bool isHovered: false

    // 组件完成时自动注册到键盘
    Component.onCompleted: {
        if (keyboard) {
            keyboard.registerInput(numberInput);
        }
    }

    // 背景
    Rectangle {
        anchors.fill: parent
        radius: borderRadius
        border.width: borderWidth
        border.color: isSelected ? selectedBorderColor
                    : isHovered ? hoverBorderColor
                    : normalBorderColor
        color: isSelected ? selectedColor : normalColor
    }

    // 悬停检测
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onEntered: isHovered = true
        onExited: isHovered = false
    }

    // 文本显示
    Text {
        anchors.fill: parent
        text: isPassword && numberInput.text.length > 0
              ? passwordMask.repeat(numberInput.text.length)
              : (numberInput.text || placeholderText)
        font.pixelSize: fontPixelSize
        color: numberInput.text ? fontColor : "#bdc3c7"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        padding: 10
    }

    // 点击交互
    MouseArea {
        anchors.fill: parent
        onClicked: {
            isSelected = true
            selectedChanged()
        }
    }
}
